﻿//---------------------------------------------------------------------------
// 
// File: HtmlTokenType.cs
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// Description: Definition of token types supported by HtmlLexicalAnalyzer
//
//---------------------------------------------------------------------------

namespace HTMLConverter.XamlConverters
{
    using System.Xml.Linq;
    using HTMLConverter.Xml;

    /// <summary>
    /// HtmlToXamlConverter is a static class that takes an HTML string
    /// and converts it into XAML
    /// </summary>
    internal class ListConverter : IConverter
    {
        const string LiItemTag = " • ";

        public Tag Type
        {
            get { return Tag.List; }
        }

        public XmlElement Convert(XNode htmlNode)
        {
            XmlElement list = new XmlElement(HtmlConverter.Xaml_Span, HtmlConverter.Xaml_Namespace);
            XmlElement htmlElement = htmlNode as XmlElement;
            if (htmlElement != null)
            {
                int n = 1;
                for (XNode htmlChildNode = htmlElement.FirstNode; htmlChildNode != null; htmlChildNode = htmlChildNode.NextNode)
                {
                    XmlElement htmlChildElement = htmlChildNode as XmlElement;
                    if (htmlChildElement != null && htmlChildElement.LowerName == "li")
                    {
                        XmlElement li = Converters.Collection[Tag.ListItem].Convert(htmlChildElement);
                        if (li != null)
                        {
                            switch (htmlElement.LowerName)
                            {
                                case "ul":
                                    list.AppendChild(WrapUlItem(li));
                                    break;
                                case "ol":
                                    list.AppendChild(WrapOlItem(li, n++));
                                    break;
                            }
                        }
                    }
                }

            }

            if (list.FirstNode != null)
            {
                // add break;
                list.AppendChild(HtmlConverter.CteateLineBreak());
                return list;
            }
            else
            {
                return null;
            }
        }

        static XmlElement WrapUlItem(XmlElement li)
        {
            XmlElement span = null;

            if (li != null)
            {
                span = new XmlElement(HtmlConverter.Xaml_Span, HtmlConverter.Xaml_Namespace);
                span.AppendChild(new XmlText(LiItemTag));
                span.AppendChild(li);
                span.AppendChild(HtmlConverter.CteateLineBreak());
            }

            return span;
        }

        static XmlElement WrapOlItem(XmlElement li, int num)
        {
            XmlElement span = null;

            if (li != null)
            {
                span = new XmlElement(HtmlConverter.Xaml_Span, HtmlConverter.Xaml_Namespace);
                span.AppendChild(new XmlText(string.Format(" {0}. ", num)));
                span.AppendChild(li);
                span.AppendChild(HtmlConverter.CteateLineBreak());
            }

            return span;
        }
    }
}
